//
//  NSString+URLParse.m
//  WebJs
//
//  Created by lizq on 16/9/12.
//  Copyright © 2016年 lizq. All rights reserved.
//

#import "NSString+URLParse.h"

@implementation NSString (URLParse)

/**
 * NSString 格式: @"method?paraments1=value1&parament2=value2&..."
 * @return NSDictionary 格式：{method:methodName,parament1:value1,...}
 */
- (NSDictionary *)urlParse {
    if(!self || self.length == 0 ) {
        return nil;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *components = [self componentsSeparatedByString:@"?"];
    NSString *method = components[0];
    NSString *paraments = components[1];
    NSArray *paramentList = [paraments componentsSeparatedByString:@"&"];
    for(NSString *parament in paramentList) {
        NSArray *keyValue = [parament componentsSeparatedByString:@"="];
        NSString *key = keyValue[0];
        NSString *value = keyValue[1];
        dic[key] = value;
    }
    dic[@"method"] = method;
    return dic;
}

@end
